package joyi.train.cheng.service.impl;

import j.data.tree.TreeData;
import j.data.tree.TreeNode;
import j.data.tree.TreeSetting;
import j.framework.core.ServiceContext;
import j.framework.core.ServiceException;
import j.util.New;
import j.util.StringUtils;
import j.util.jdbc.RowMapper;
import j.util.pkgenerator.PG;
import joyi.train.cheng.domain.City;
import joyi.train.cheng.service.CityRequestService;
import sun.reflect.generics.tree.Tree;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by WY2015092804 on 2015/10/15.
 */
public class CityRequestServiceImpl implements CityRequestService {

    private static RowMapper<City> rowMapper = new RowMapper<City>() {
        @Override
        public City mapRow(ResultSet rs, int rowNum) throws SQLException {
            City city = new City();
            city.setCityId(rs.getString("cityId"));
            city.setParentId(rs.getString("parentId"));
            city.setNote(rs.getString("note"));
            city.setCityName(rs.getString("cityName"));
            return city;
        }
    };

    @Override
    public Map<String, Object> getCityTreegrid(ServiceContext serviceContext) {
        Map<String, Object> map = New.map();
        List<Map<String, Object>> list = serviceContext.getJdbcTemplate().queryForListMap("select cityId id,cityName name,parentId _parentId from cheng_City");
        map.put("rows", list);
        map.put("total", list.size());

        return map;
    }

    @Override
    public City getCity(ServiceContext serviceContext) {
        String cityId = serviceContext.params.getString("cityId");
        if (StringUtils.isNullOrEmpty(cityId)) {
            return null;
        }
        City city = serviceContext.getJdbcTemplate().queryForDomain("select * from cheng_City where cityId = ?", rowMapper, cityId
        );
        return city;
    }

    @Override
    public Map<String, Object> delCity(ServiceContext serviceContext) {
        String cityId = serviceContext.params.getString("cityId");
        if (StringUtils.isNullOrEmpty(cityId)) {
            serviceContext.actionResult.add("操作有误");
            return null;
        }
        if (serviceContext.getJdbcTemplate().queryForInt("select count(*) from cheng_City where parentId= ?", cityId) > 0) {
            serviceContext.actionResult.add("有下属地区");
            return null;
        }
        if (serviceContext.getJdbcTemplate().queryForInt("select count(*) from cheng_City_Customer where cityId = ?", cityId) > 0) {
            throw new ServiceException("地区有客户");
        }
        City city = new City();
        city.setCityId(cityId);
        Integer i = serviceContext.getJdbcTemplate().delete(city);
        if (i == 0) {
            throw new ServiceException("此地区已经删过了");
        } else {
            Map<String, Object> map = New.map();
            map.put("da", "删除成功");
            return map;
        }

    }

    @Override
    public void updateCity(ServiceContext serviceContext) {

    }

    @Override
    public TreeData getCityTree(ServiceContext serviceContext) {
        List<City> list = serviceContext.getJdbcTemplate().queryForList("select * from cheng_City",rowMapper);
        TreeSetting setting = new TreeSetting(false,false);
        setting.setNameFieldName("text");
        TreeData treeData = new TreeData(setting);
        for (City city : list){
            TreeNode treeNode = new TreeNode(city.getCityId(),city.getCityName());
            treeNode.setpId(city.getParentId());
            treeData.addNode(treeNode);
        }
        return treeData;
    }

    @Override
    public void addCity(ServiceContext serviceContext) {
    }

    @Override
    public void saveOrUpdateCity(ServiceContext serviceContext) {
        City city = serviceContext.getBean(City.class);
        city.setNote(city.getNote().trim());
        if (StringUtils.isNullOrEmpty(city.getCityId())) {
            city.setCityId(PG.getNext("CIT"));
            city.setAddedUserId(serviceContext.getUser().getUserId());
            city.setAddedTime(new Date());
            serviceContext.getJdbcTemplate().insert(city);
            return;
        }
        if(city.getCityId().equals(city.getParentId())){
            throw new ServiceException("地区所属地区不能是其本身");
        }
        serviceContext.getJdbcTemplate().update(city);
    }
}
